﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace MiniBotAlgorithmLibSample
{
    public partial class ApiSamples
    {
        #region 静态公共方法

        internal static IntPtr ArrayToIntptr(byte[] source)
        {
            if (source == null)
                return IntPtr.Zero;
            unsafe
            {
                fixed (byte* point = source)
                {
                    IntPtr ptr = new IntPtr(point);
                    return ptr;
                }
            }
        }

        internal static IntPtr IntArrayToIntptr(int[] source)
        {
            if (source == null)
                return IntPtr.Zero;
            unsafe
            {
                fixed (int* point = source)
                {
                    IntPtr ptr = new IntPtr(point);
                    return ptr;
                }
            }
        }


        internal static IntPtr IntArrayToIntptr(Int64[] source)
        {
            if (source == null)
                return IntPtr.Zero;
            unsafe
            {
                fixed (Int64* point = source)
                {
                    IntPtr ptr = new IntPtr(point);
                    return ptr;
                }
            }
        }



        public static byte[] IntptrToArray(IntPtr intPtr, int size)
        {
            byte[] resultData = new byte[size];
            Marshal.Copy(intPtr, resultData, 0, size);
            return resultData;
        }


        public static float[] IntptrToFloatArray(IntPtr intPtr, int size)
        {
            float[] resultData = new float[size];
            Marshal.Copy(intPtr, resultData, 0, size);
            return resultData;
        }

        public static double[] IntptrToDoubleArray(IntPtr intPtr, int size)
        {
            double[] resultData = new double[size];
            Marshal.Copy(intPtr, resultData, 0, size);
            return resultData;
        }



        internal static IntPtr FloatArrayToIntptr(float[] source)
        {
            if (source == null)
                return IntPtr.Zero;
            unsafe
            {
                fixed (float* point = source)
                {
                    IntPtr ptr = new IntPtr(point);
                    return ptr;
                }
            }
        }


        #endregion
    }
}
